Error 2185, you can't reference a property or method for a control unless the control has the focus.


Run time error 2185, you can't reference a property or method for a control unless the control has the focus. Error 2185 error is very common. In Access VBA the Text property of text-box is only available when the control has focus. The Text property is rarely used in VBA. Use the Value property of text-box instead of text property to avoid error 2185. When we are using value property of text-box, then there is no need to set focus and since it is the default property for a text box. In this article we discuss both the properties:

Text-box text property: Text property is working only when the text-box has focus. We can set the focus on text-box using set focus property otherwise it will throw an error as shown in Fig 1.1. Text property gives you what is displayed on the screen. To move the focus to a control, we can use the SetFocus method or GoToControl action.

How to fix Error 2185 in MS Access VBA Fig-1.1

Fig:-1.1

Text-box Value property: We can use value property of text-box without set focus. Value property gives you the underlying value. We can use the Value property to determine whether a control, the selected value in the control, the text contained in a text box control, or the value of a custom property is selected. The Value property returns or sets the default property of a control, which is the property that is assumed when no property name is specified explicitly. In the following example, because the default value of the text box is the value of the Text property can refer to the value of the text property without explicitly specifying the name of the property.

VBA code to reproduce error:

Option Compare Database
Private Sub Form_Current()
'Me!Text0.SetFocus ' Uncomment the line to remove the error 2185
Dim nval As String
nval = Me!Text1
Me!Text0.Text = nval
End Sub

VBA code using Value property:

Option Compare Database
Private Sub Form_Current()
Dim nval As String
nval = Me!Text1
Me!Text0.Value = nval
End Sub


DISCLAIMER

It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. ERP Makers will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.


 

BUY SERVICES CONTACT